package com.ag.oa.core.controller;

import com.ag.oa.core.entity.vo.NewsVO;
import com.ag.oa.core.service.INewsService;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping({"/news", "/test/news"})
public class NewsController {

    @Autowired
    private INewsService newsService;

    private static final Logger logger = LoggerFactory.getLogger(NewsController.class);

    @RequestMapping(value = "/pageList", produces = "text/json;charset=UTF-8")
    public @ResponseBody String getNewsData(Integer topNum, Integer page, Integer pageSize) {

        JSONObject res = new JSONObject();
        int code = 200;
        try {
            List<NewsVO> voList = newsService.getForNewsDataList(topNum, page, pageSize);
            res.put("newsArrList", voList);
        } catch (Exception e) {
            e.printStackTrace();
            code = 500;
            logger.error(e.getMessage());
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

    /**
     * 根据 id 获取信息
     */
    @RequestMapping(value = "/newsInfo", produces = "text/json;charset=UTF-8")
    public @ResponseBody String getNewsInfo(Integer id) {

        JSONObject res = new JSONObject();
        int code = 200;
        try {
            NewsVO vo = newsService.getNewsInfoById(id);
            res.put("newsInfo", vo);
        } catch (Exception e) {
            e.printStackTrace();
            code = 500;
            logger.error(e.getMessage());
        }
        res.put("resCode", code);
        return res.toJSONString();
    }

}
